package com.ndp.fb.scheduler.job.business;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import com.alibaba.dubbo.config.annotation.Reference;
import com.ndp.fb.constant.ConfigConst;
import com.ndp.fb.data.api.service.schedulerservice.business.syncbusiness.FaceBookJobService;
import com.ndp.fb.data.api.service.schedulerservice.business.syncbusiness.SyncBusinessService;
import com.ndp.fb.mq.api.service.schedulerservice.FBUserSchedulerService;
import com.ndp.fb.mq.api.service.schedulerservice.business.syncbusiness.SyncAccountInfoService;
import com.ndp.fb.rdb.api.FbUserRdbService;
import com.ndp.fb.rdb.model.FbUser;
import com.ndp.fb.scheduler.annotation.CronScheduled;
import com.ndp.fb.util.ThreadPool;
import com.restfb.util.StringUtils;

/**
 * Created by Mikey.zhaopeng on 2015/3/10 14:16.
 * Email mikey.zhao@ndpmedia.com
 * 业务数据同步 job
 */
@Component
public class SyncBusinessJob {
    private final Logger logger = LoggerFactory.getLogger(getClass());
    @Reference
    FbUserRdbService fbUserRdbService;
    @Reference
    FBUserSchedulerService fbUserSchedulerService;
    @Reference
    SyncBusinessService syncBusinessService;
    @Reference
    private SyncAccountInfoService syncAccountInfoService;
    @Reference
    FaceBookJobService faceBookJobService;
    /**
     * 执行界面上的任务
     */
    @CronScheduled(cronkey = ConfigConst.SCHEDULE_RUN_BUSINESS_JOB_CRON, enablekey = ConfigConst.SCHEDULE_RUN_BUSINESS_JOB_ENABLE)
    public void runJob() {
        logger.info("开始执行Facebookjob");
        faceBookJobService.runJob();
    }
    /**
     * 同步账号的LOG信息
     */
    @CronScheduled(cronkey = ConfigConst.SCHEDULE_SYNC_BUSINESS_ADACCOUNT_ACTIVILYLOG_CRON, enablekey = ConfigConst.SCHEDULE_SYNC_BUSINESS_ADACCOUNT_ACTIVILYLOG_ENABLE)
    public void syncAccountLog() {
        logger.info("开始同步账号的LOG信息");
        syncBusinessService.syncAllAccountActivityLog();
    }
    /**
     * 同步账号的LOG信息
     */
    @CronScheduled(cronkey = ConfigConst.SCHEDULE_SYNC_BUSINESS_ADACCOUNT_ACTIVILYLOG_CRON, enablekey = ConfigConst.SCHEDULE_SYNC_BUSINESS_ADACCOUNT_ACTIVILYLOG_ENABLE)
    public void syncCampaignGroupLog() {
        logger.info("开始同步账号的LOG信息");
        syncBusinessService.syncAllAccountActivityLog();
    }
    /**
     * 同步账号的LOG信息
     */
    @CronScheduled(cronkey = ConfigConst.SCHEDULE_SYNC_BUSINESS_ADACCOUNT_CHECKDATA_CRON, enablekey = ConfigConst.SCHEDULE_SYNC_BUSINESS_ADACCOUNT_CHECKDATA_ENABLE)
    public void syncCheckAllAccountData() {
        logger.info("开始验证账号数据的完整性");
        syncBusinessService.syncCheckAllAccountData();
    }
    /**
     * 初始化新创建的广告的imageInfo字段
     */
    @CronScheduled(cronkey = ConfigConst.SCHEDULE_SYNC_BUSINESS_INIT_IMAGEINFO_CRON, enablekey = ConfigConst.SCHEDULE_SYNC_BUSINESS_INIT_IMAGEINFO_ENABLE)
    public void initImageHash() {
        logger.info("开始初始化新创建的广告的imageInfo字段");
        syncBusinessService.initImageHash();
    }
    /**
     * 初始化新创建的广告的imageInfo字段
     */
    @CronScheduled(cronkey = ConfigConst.SCHEDULE_SYNC_BUSINESS_PEFECT_IMAGEINFO_CRON, enablekey = ConfigConst.SCHEDULE_SYNC_BUSINESS_PEFECT_IMAGEINFO_ENABLE)
    public void perfectImageHash() {
        logger.info("开始计算新创建的广告的imageHash字段");
        syncBusinessService.perfectImageHash();
    }
    /**
     * 同步ad相关的数据，包括adGroup,adSet,campaignGroup,adCreative
     */
    @CronScheduled(cronkey = ConfigConst.SCHEDULE_SYNC_BUSINESS_AD_V24_CRON, enablekey = ConfigConst.SCHEDULE_SYNC_BUSINESS_AD_V24_ENABLE)
    public void syncAdV24() {
        logger.info("开始同步ad相关的数据，包括adGroup,adSet,campaignGroup,adCreative");
        syncBusinessService.syncAllAccountData();
    }

    /**
     * 同步 adCreative
     */
    @CronScheduled(cronkey = ConfigConst.SCHEDULE_SYNC_BUSINESS_AD_ACCOUNT_CRON, enablekey = ConfigConst.SCHEDULE_SYNC_BUSINESS_AD_ACCOUNT_ENABLE)
    public void syncAdAccount() {
        logger.info("开始同步adAccount下面的connectionobject");
//        syncBusinessService.syncAccountApp();
        //syncAccountInfoService.syncAccountInfo();
    }

    /**
     * 同步 adCreative
     */
    @CronScheduled(cronkey = ConfigConst.SCHEDULE_SYNC_BUSINESS_PAGE_CRON, enablekey = ConfigConst.SCHEDULE_SYNC_BUSINESS_PAGE_ENABLE)
    public void syncPageInfo() {
        logger.info("开始同步主页信息");
        syncAccountInfoService.syncPagesInfo();
    }

    /**
     * 同步 adCreative
     */
    @CronScheduled(cronkey = ConfigConst.SCHEDULE_SYNC_BUSINESS_PERMISSIONS_CRON, enablekey = ConfigConst.SCHEDULE_SYNC_BUSINESS_PERMISSIONS_ENABLE)
    public void syncPermissions() {
        logger.info("开始同步权限信息");
        syncAccountInfoService.syncPermission();
    }


    /**
     * 清除缓存中AccessToken的状态
     */
    @CronScheduled(cronkey = ConfigConst.CLEAR_FBUSER_ACCESSTOKEN_STATUS_CACHE_CRON, enablekey = ConfigConst.CLEAR_FBUSER_ACCESSTOKEN_STATUS_CACHE_ENABLE)
    public void clearFbUserAccessTokenStatusCache() {
        logger.info("start clearFbUserAccessTokenStatusCache");
        List<FbUser> userList = fbUserRdbService.getAllFbUser();
        if (CollectionUtils.isEmpty(userList)) {
            return;
        }
        for (final FbUser user : userList) {
            if (user == null || StringUtils.isBlank(user.getAccessToken())) {
                continue;
            }
            ThreadPool.execute(() -> {
                logger.info("清理:" + user.getFbUserId());
                fbUserRdbService.clearValidByFbUser(user);
            });
        }
    }
}
